***Note: replicators should set directory to open data 
*cd ""


* first, get data from Eurostat.
*ssc install eurostatuse

*january 2024 - may change location on eurostat website

/*
clear all 
*gender pay gap
eurostatuse edat_lfse_03, long label noflags  
rename time year
keep if year>2001
rename geo_label country 
keep if sex=="T" 
rename edat_lfse_03 Edu2
keep if age=="Y15-64"
keep if isced11=="ED3-8"  
keep geo country year Edu2 isced11
rename geo cntry
replace cntry="GR" if cntry=="EL"
replace country="Germany" if country=="Germany (until 1990 former territory of the FRG)"
replace country="Czech Republic" if country=="Czechia"
save eurostat.dta, replace
*/
*Now back to main dataset - merge later

clear all
 use "ESS.dta"
 *focus on sample from countries with monotonic first stage
keep if Monotonic==1 

*education level for each subgroup
g EducationI=Education if Ingroup==1
g EducationO=Education if Ingroup==0
g EducationM=Education if Female==0
g EducationW=Education if Female==1


*Harmonize discriminatory gender attitudes across the two proxies in order to gain some data about the overall trajectory and avoid having a tendency line that only hinges on two data points
*first rescale the second proxy (referred to as 'altenative measure' in the paper) in the 0-1
g D_gender2=(5-wmcpwrk)/4

*then create a single item combining main and alternative proxy
g D_genderU=D_gender
replace D_genderU=D_gender2 if D_genderU==.
 
*Now collapse at country year
collapse Education EducationI EducationO EducationM EducationW total_eduyrs   PD_race PD_gender D_gender D_genderU D_race  D_race2 if Monotonic==1, by(country year)

*merge with eurostat data - check representativeness of ESS
merge 1:n  country year  using "eurostat.dta"  
keep if _merge==3

*standardize and label variables
foreach var in  Edu2 Education EducationI EducationO EducationM EducationW  total_eduyrs   PD_race PD_gender D_gender D_genderU D_race {
		qui sum `var'
		replace `var' = (`var' - `r(mean)') / (`r(sd)')
	}

la var Education "Education"
la var EducationI "Education majority"
la var EducationO "Education minority"
la var EducationM "Education men"
la var EducationW "Education women"


*graphs 6.2 coding
   		   grstyle init
	 grstyle set graphsize 13cm 16cm
twoway ( lfit  Edu2  year,   lpattern("_._.") lcolor(black) lw(medthick)    )  ///
 ( lfit EducationI  year,   lpattern(dot) lcolor(black) lw(medthick)   ) ///
  ( lfit EducationO  year,   lpattern(dash) lcolor(black) lw(medthick)   ) ///
   ( lfit PD_race  year,   lpattern(solid) lcolor(black%50) lw(medthick)  )  ///
   ( lfit D_race year,   lpattern(dash) lcolor(black%50)  lw(medthick)  )   , legend( order(1 "Education (Eurostat)" 2 "Education (ESS, majority)" 3 "Education (ESS, minority)" 4 "Perceptions of discrimination" 5 "Discriminatory attitudes") rows(2) position(12) size(*.72) symxsize(*0.35) region(lstyle(none))  span )  ylabel(-1.2(0.4)1.2,  labsize(medium)) yscale(range(-1.2(0.4)1.2))  xlabel(2002(4)2018,  labsize(medium)) xscale(range(2002(4)2018)) ysize(20) xsize(16) title("{bf: Ethnoracial discrimination}") ytitle("Standardized country-year average", size(4.5)) xtitle("Year of survey") ysc(titlegap(4) outergap(1)) note("")
graph save "Graph/Figure10a", replace 

  
   		   grstyle init
	 grstyle set graphsize 13cm 16cm
twoway ( lfit  Edu2  year,   lpattern("_._.") lcolor(black) lw(medthick)    )  ///
 ( lfit EducationM  year,   lpattern(dot) lcolor(black) lw(medthick)   ) ///
  ( lfit EducationW  year,   lpattern(dash) lcolor(black) lw(medthick)   ) ///
   ( lfit PD_gender  year,   lpattern(solid) lcolor(black%50) lw(medthick)  )  ///
   ( lfit D_gender year,   lpattern(dash) lcolor(black%50)  lw(medthick)  )   , legend( order(1 "Education (Eurostat)" 2 "Education (ESS, men)" 3 "Education (ESS, women)" 4 "Perceptions of discrimination" 5 "Discriminatory attitudes") rows(2) position(12) size(*.72) symxsize(*0.35) region(lstyle(none)) span  )  ylabel(-1.2(0.4)1.2,  labsize(medium)) yscale(range(-1.2(0.4)1.2))  xlabel(2002(4)2018,  labsize(medium)) xscale(range(2002(4)2018)) ysize(20) xsize(16) title("{bf: Gender discrimination}") ytitle("") xtitle("Year of survey") ysc(titlegap(4) outergap(1)) note("")
graph save "Graph/Figure10b", replace 

      		   grstyle init
	 grstyle set graphsize 13cm 24cm
 graph combine "Graph/Figure10a.gph" "Graph/Figure10b.gph",  cols(2) row(1) imargin(l=0 r=0 b=0) ycommon xcommon 
 graph export "Graph/Figure6_2.tif", replace
 erase "Graph/Figure10a.gph"
 erase "Graph/Figure10b.gph"
